<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Course as CourseModel;
use app\common\model\user\Favorite;

/**
 * 首页接口
 */
class Activity extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function list()
    {
        $model = new \app\common\model\Activity();
        $list = $model->order(['updatetime' => 'desc'])->select();
        $this->success('', $list);
    }

    public function info()
    {
        $id = $this->request->get('id', 0);
        $model = new \app\common\model\Activity();
        $info = $model->withCount('userCount')->find($id);
        $info->user_count = $info->user_count_count;
        unset($info->user_count_count);
        if (!$info) {
            $this->error('活动不存在');
        }
        $info->is_apply = false;

        $user_id = $this->auth->id;
        if ($user_id) {
            $userAct = \app\common\model\user\Activity::where(['user_id' => $user_id, 'activity_id' => $id, 'status' => \app\common\model\user\Activity::STATUS_SUCC])->find();
            $info->is_apply = $userAct ? true : false;
        }
        $this->success('', $info);
    }
}
